<?php

namespace App\Admin\Controllers;

use App\Admin\BaseAdmin;
use App\Http\Controllers\Controller;
use App\Models\Doc;
use App\Models\Tag;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;

class DocController extends Controller
{
    use ModelForm, BaseAdmin;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('文档');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Doc::class, function (Grid $grid) {

            $status = request('status', 1);
            switch ($status) {
                case 0:
                    $grid->model()->pending();
                    break;
                case 2:
                    $grid->model()->rejected();
                    break;
            }
            $grid->model()->orderBy("id", "desc");
            $grid->paginate(10);
            $grid->column('id', 'ID')->sortable();
            $grid->column('cover', '文档封面')->style("width:60px;")->thumb(40, intval(40 / 0.75));
            $grid->column('title', '文档标题');
            $grid->category()->name('文档类型');
            $grid->user()->name('创建用户')->label('default');
            /*$grid->column('abstract', '简介')->display(function ($data) {
                return str_limit($data, 30);
            });*/
            $grid->column('doc_type', '付费类型')->type_name(Doc::DOC_TYPE);
            $grid->column('money', '价格')->sortable();
            $grid->pages_count('文档数量')->display(function () {
                return collect($this->pages)->count();
            })->label();
            $grid->tags('标签')->display(function ($tags) {
                $tags = array_map(function ($tag) {
                    return "<span class='label label-success'>{$tag['name']}</span>";
                }, $tags);
                return join(" ", $tags);
            });

            $grid->column('approved', '上下架')->switch();
            $grid->column('hot', '推荐排序')->editable()->sortable();
            $grid->column('home', '首页排序')->editable()->sortable();
            $grid->column('nav_color', '导航栏')->editable();
            $grid->column('font_color', '字体黑色')->switch();
            $grid->column('view', '访问量')->sortable();
            $grid->column('created_at', '创建时间')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->append('<a href="' . route('docs.edit', ['id' => $actions->getKey()]) . '" class="btn btn-xs">编辑</a>');
                $actions->append('<a href="' . route('admin.doc-check', ['id' => $actions->getKey()]) . '" class="btn btn-xs">审核</a>');
            });

            $grid->disableRowSelector();
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->tools(function (Grid\Tools $tools) {
                $tools->disableRefreshButton();
                $tools->append('<div class="btn-group">');
                $tools->append('<a href="' . admin_url('docs?status=1') . '" class="btn btn-default btn-sm">已通过</a>');
                $tools->append('<a href="' . admin_url('docs?status=0') . '" class="btn btn-default btn-sm">待审核</a>');
                $tools->append('<a href="' . admin_url('docs?status=2') . '" class="btn btn-default btn-sm">不通过</a>');
                $tools->append('</div>');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Doc::class, function (Form $form) {

            $form->text('title', '文档标题');
            $form->textarea('abstract', '简介');
            $form->image('cover', '文档封面');
            $form->switch('approved', '上下架');
            $form->text('hot', '推荐排序');
            $form->text('home', '首页排序');
            $form->text('nav_color', '导航栏');
            $form->switch('font_color', '字体黑色');

            $form->multipleSelect('tags')->options(Tag::all()->pluck('name', 'id'));

        });
    }

    public function check(Request $request)
    {
        $id = $request->input("id");
        $doc = Doc::query()->withAnyStatus()->findOrFail($id);
        if ($request->isMethod('post')) {
            $status = $request->input("status", 0);
            $re_back = $request->input("re_back");
            if ($status == 1) {
                Doc::approve($doc->id);
            } else {
                Doc::reject($doc->id);
            }
            admin_toastr('审核成功');

            return redirect($re_back);
        } else {
            return Admin::content(function (Content $content) use ($doc) {
                $form = new \Encore\Admin\Widgets\Form();
                $form->action(route('admin.doc-check', ['id' => $doc->id]));
                $form->method('post');
                $form->display('title', '审核文档')->default($doc->title);
                $form->display('id', '文档ID')->default($doc->id);
                $form->display('zt', '当前状态')->default($doc->status);
                $form->hidden('re_back', '')->default(url()->previous());
                $form->radio('status', '审核状态')->options([1 => '通过', 2 => '不通过'])->default(1);
                $content->body(new Box('审核文档', $form));
            });
        }
    }
}
